const redisUtil = require('../../framework/lib/redis/redisUtil')
const amqp_cfg = configure.get('amqp')
const im_suffix = amqp_cfg.im_suffix || ''
const _service_reg = module.exports

_service_reg.reg = async (app_key, data) => {
    if (!_.isArray(data)) data = [data]

    let _service = await dbUtil.getRow('service_reg', { app_key: app_key, im_suffix: im_suffix })
    if (!_service) return false
    _service.list = _service.list || []
    data = data.filter(item => {
        let _item_unit_id = _.get(item, 'unit_id') || ''
        let _item_device_id = _.get(item, 'device_id') || ''
        let _item_event_id = _.get(item, 'event_id') || ''
        let _item_region_code = _.get(item, 'region_code') || ''

        if (!_item_unit_id && !_item_device_id && !_item_event_id && !_item_region_code) {
            return false
        }

        let _index = _service.list.findIndex(fi => {
            let _fi_unit_id = _.get(fi, 'unit_id') || ''
            let _fi_device_id = _.get(fi, 'device_id') || ''
            let _fi_event_id = _.get(fi, 'event_id') || ''
            let _fi_region_code = _.get(fi, 'region_code') || ''

            return _item_unit_id === _fi_unit_id && _item_device_id === _fi_device_id && _item_event_id === _fi_event_id && _item_region_code === _fi_region_code
        })

        return !~_index
    })

    if (_.isEmpty(data)) return true

    return M('service_reg').updateOne({ _id: _service._id }, {
        $addToSet: {
            list: { $each: data },
        }
    })
}

_service_reg.unReg = async (app_key, data) => {
    if (!_.isArray(data)) data = [data]

    let _service = await dbUtil.getRow('service_reg', { app_key: app_key, im_suffix: im_suffix })

    if (!_service) return false

    _service.list = _service.list || []

    _service.list = _service.list.filter(item => {
        let _item_unit_id = _.get(item, 'unit_id') || ''
        let _item_device_id = _.get(item, 'device_id') || ''
        let _item_event_id = _.get(item, 'event_id') || ''
        let _item_region_code = _.get(item, 'region_code') || ''

        let _index = data.findIndex(fi => {
            let _fi_unit_id = _.get(fi, 'unit_id') || ''
            let _fi_device_id = _.get(fi, 'device_id') || ''
            let _fi_event_id = _.get(fi, 'event_id') || ''
            let _fi_region_code = _.get(fi, 'region_code') || ''

            return _item_unit_id === _fi_unit_id && _item_device_id === _fi_device_id && _item_event_id === _fi_event_id && _item_region_code === _fi_region_code
        })

        return !!~_index
    })

    return M('service_reg').updateOne({ _id: _service._id }, {
        $pull: {
            list: {
                _id: { $in: _service.list.map(m => m._id) }
            },
        }
    })
}

_service_reg.regList = async (args = {}) => {
    let _prefix = `service_reg.regList.` + im_suffix

    let _redis_get = await redisUtil.get(_prefix)

    if (_redis_get) {
        return _redis_get
    } else {
        let _services = await dbUtil.getAll('service_reg', { im_suffix: im_suffix })
        if (!_services) return null

        await redisUtil.set(_prefix, _services, 1)
        return _services
    }
}
